package com.mystaggeredview.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.mystaggeredview.R;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Auth Mr.lc(兰超)
 * @Date 2018/4/25
 * @Time 14:00
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    public OnItemClick onItemClick;
    public interface OnItemClick{
        public void OnClick(View view,int position);
    }
    public Context mContex;
    private String[] images;
    private List<Integer> mHeights;
    private HashMap<Integer,Integer> imageHeightMap=new HashMap();
    private int imageWidth=0;
    public RecyclerAdapter(Context mContex,String[] images){
        this.mContex=mContex;
        this.images=images;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContex).inflate(R.layout.recyclerview_item, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (!this.imageHeightMap.containsKey(position)){
//当首次加载图片时，调用 loadImageFirst()，保存图片高度
            loadImageFirst(holder.imageView,position);
        }else{
//非首次加载，直接根据保存的长宽，获取图片
            Glide.with(mContex)
                    .load(images[position]).override(this.imageWidth,this.imageHeightMap.get(position))
                    .into(holder.imageView);
        }
        Glide.with(mContex).load(images[position]).into(holder.imageView);
        holder.textView.setText(position+"");
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.OnClick(holder.imageView,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return images.length;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_show)
        TextView textView;
        @BindView(R.id.iv_show)
        ImageView imageView;
        @BindView(R.id.cardview)
        CardView cardView;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public void loadImageFirst(View view, final int position){
        //构造方法中参数view,就是回调方法中的this.view
        ViewTarget<View,Bitmap> target = new ViewTarget<View, Bitmap>(view) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                //加载图片成功后调用
                float scaleType = ((float) resource.getHeight())/resource.getWidth();
                int imageHeight = (int) (imageWidth*scaleType);
                //获取图片高度，保存在Map中
                imageHeightMap.put(position,imageHeight);
                //设置图片布局的长宽，Glide会根据布局的自动加载适应大小的图片
                ViewGroup.LayoutParams lp = this.view.getLayoutParams();
                lp.width=imageWidth;
                lp.height=imageHeight;
                this.view.setLayoutParams(lp);
                //resource就是加载成功后的图片资源
                ((ImageView)view).setImageBitmap(resource);
            }
            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                //加载图片失败后调用
                super.onLoadFailed(e, errorDrawable);
                int imageHeight = imageWidth;
                imageHeightMap.put(position,imageHeight);
                ViewGroup.LayoutParams lp = this.view.getLayoutParams();
                lp.width=imageWidth;
                lp.height=imageHeight;
                this.view.setLayoutParams(lp);
                ((ImageView)view).setImageResource(R.mipmap.ic_launcher);
            }
        };
        Glide.with(mContex)
                .load(images[position])
                .asBitmap()                 //作为Bitmap加载，对应onResourceReady回调中第一个参数的类型
                .into(target);
    }
    public void setOnItemClick(OnItemClick onItemClick){
        this.onItemClick=onItemClick;
    }
}
